/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BLL;

import DAL.ConnectUnit;
import DTO.SavingType_DTO;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.HashSet;

/**
 *
 * @author Nguyen Phi Hung
 */
public class SavingType_BLL {
    
    ConnectUnit connection;
    
    public SavingType_BLL(){
        this.connection = DAL.DAL.getDAL();
    }
    
    /**
     * get list of SavingType form database with Condition and OrderBy
     * @param condition
     * @param OrderBy
     * @return
     * @throws Exception 
     */
    public HashSet<SavingType_DTO> getSavingTypes(String condition, String OrderBy) throws Exception{
        HashSet<SavingType_DTO> savingtypes = new HashSet<SavingType_DTO>();
        ResultSet result = this.connection.Select("SAVINGTYPE", condition, OrderBy);
        while(result.next()){
            SavingType_DTO savingtype = new SavingType_DTO();
            savingtype.setTypeID(result.getString("TypeID"));
            savingtype.setTerm(result.getInt("Term"));
            savingtype.setName(result.getString("TypeName"));
            savingtype.setInterestRate(result.getFloat("AllowWithdraw"));
            savingtype.setAllowSave(result.getBoolean("AllowSave"));
            savingtypes.add(savingtype);
        }
        return savingtypes;
    }
    
    /**
     * get list of SavingType form database with Condition
     * @param condition
     * @return
     * @throws Exception 
     */
    public HashSet<SavingType_DTO> getSavingTypes(String condition) throws Exception{
        return this.getSavingTypes(condition, null);
    }
    
    /**
     * get list of SavingType form database
     * @return
     * @throws Exception 
     */
    public HashSet<SavingType_DTO> getSavingTypes() throws Exception{
        return this.getSavingTypes(null, null);
    }
    
    /**
     * get SavingType by TypeID
     * @param ID
     * @return
     * @throws Exception 
     */
    public SavingType_DTO getSavingType_byTypeID(String ID) throws Exception{
        HashSet<SavingType_DTO> savingtype = this.getSavingTypes("TypeID= '"+ ID+ "'");
        return (savingtype.size()>0)? savingtype.toArray(new SavingType_DTO[savingtype.size()])[0]: null;   
    }
    
    /**
     * get SavingType by TypeName
     * @param typename
     * @return
     * @throws Exception 
     */
    public SavingType_DTO getSavingType_byTypeName(String typename) throws Exception{
        HashSet<SavingType_DTO> savingtype = this.getSavingTypes("TypeName= N'"+ typename+ "'");
        return (savingtype.size()>0)? savingtype.toArray(new SavingType_DTO[savingtype.size()])[0]: null;   
    }
    
    /**
     * get SavingType by index
     * @param list
     * @param index
     * @return 
     */
    public SavingType_DTO getSavingType_byIndex(HashSet<SavingType_DTO> list, int index){
      return (index > list.size()) ? null : list.toArray(new SavingType_DTO[list.size()])[index];
    }
    /**
     * Insert a SavingType into database
     * @param info
     * @throws Exception 
     */
    public void Insert(SavingType_DTO info) throws Exception{
        HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("TypeID", info.getTypeID());
        map.put("Term", info.getTerm());
        map.put("Name", info.getName());
        map.put("AllowWithdraw", info.isAllowWithdraw());
        map.put("AllowSave", info.isAllowSave());
        
        String[] arr = {"TypeID","Term","Name", "AllowWithdraw", "AllowSave"};
        this.connection.Insert("SAVINGTYPE", map, arr);
    }
    
    /**
     * Update a SavingType by TypeID into database
     * @param info
     * @throws Exception 
     */
    public void Update(SavingType_DTO info) throws Exception{
         HashMap<String, Object> map = new HashMap<String, Object>();
        
        map.put("TypeID", info.getTypeID());
        map.put("Term", info.getTerm());
        map.put("Name", info.getName());
        map.put("AllowWithdraw", info.isAllowWithdraw());
        map.put("AllowSave", info.isAllowSave());
        
        this.connection.Update("SAVINGTYPE", map, "TypeID="+info.getTypeID());
    }
    
    /**
     * Delete a SavingType by TypeID in database
     * @param info
     * @throws Exception 
     */
    public void Delete(SavingType_DTO info) throws Exception{
        this.connection.Delete("SAVINGTYPE", "TypeID="+info.getTypeID());
    }
}
